package com.alibaba.shopping_manager_api.controller;

import com.alibaba.shopping_common.pojo.ProductType;
import com.alibaba.shopping_common.result.BaseResult;
import com.alibaba.shopping_common.service.ProductTypeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName ProductTypeController
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/9 12:38
 */
@RestController
@RequestMapping("/productType")
public class ProductTypeController {

    @DubboReference
    private ProductTypeService productTypeService;

    /**
     * 新增产品类型
     * @param productType
     */
    @PostMapping("/add")
    public BaseResult<ProductType> add(@RequestBody ProductType productType){
        System.out.println(productType);
        if (productType == null || !StringUtils.hasText(productType.getName())){
            return BaseResult.ERR_PARAMETER_IS_NULL(productType);
        }
        productTypeService.add(productType);
        return BaseResult.OK(productType);
    }

    @DeleteMapping("/delete")
    public BaseResult<Long> delete(Long id){
        try {
            // 如果ID<=0直接返回
            if (id <= 0){
                return BaseResult.ERR_PARAMETER_IS_NULL(id);
            }
            productTypeService.del(id);
            return BaseResult.OK(id);
        } catch (RuntimeException e) {
            return BaseResult.ERR_SYS(null);
        }
    }

    @PutMapping("/update")
    public BaseResult<ProductType> update(@RequestBody ProductType productType){
        // 如果ID<=0直接返回,证明前端传入错误
        if (productType.getId() <= 0 ){
            return BaseResult.ERR_PARAMETER_IS_NULL(productType);
        }
        productTypeService.update(productType);
        return BaseResult.OK(productType);
    }

    /**
     * 查询所有的产品类型数据
     * @return
     */
    @GetMapping("/all")
    public BaseResult<List<ProductType>> all(ProductType productType){
        try{
            List<ProductType> all = productTypeService.all(productType);
            return BaseResult.OK(all);
        } catch (RuntimeException e) {
            return BaseResult.ERR_SYS(null);
        }
    }

    /**
     * 根据ID查询产品类型详细信息
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public BaseResult<ProductType> findById(Long id){
        ProductType productType = productTypeService.findById(id);
        return BaseResult.OK(productType);
    }
    /**
     * 分页查询
     * @param productType
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search")
    public BaseResult<Page<ProductType>> search(ProductType productType,
                                                @RequestParam(value = "page", defaultValue = "1") int page,
                                                @RequestParam(value = "size", defaultValue = "10") int size){
        try{
            System.out.println(productType);
            System.out.println(page);
            System.out.println(size);
            Page<ProductType> page1 = productTypeService.search(productType, page, size);
            System.out.println(page1);
            return BaseResult.OK(page1);
        } catch (RuntimeException e) {
            return BaseResult.ERR_SYS(null);
        }
    }

    // 根据ID查询所有的子类型
    @GetMapping("/findByParentId")
    public BaseResult<List<ProductType>>findByParentId(ProductType productType){
        List<ProductType> byParentId = productTypeService.findByParentId(productType);
        return BaseResult.OK(byParentId);
    }
}




